<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>用户名<input type="text" id="username" autocomplete="off" /></div>
    <div>密码<input type="text" id="password" autocomplete="off" /></div>
    <button>登录</button>
    <span id="span"></span>
    <script>
      document.querySelector("button").addEventListener("click", function () {
        fetch("/login", {
          method: "POST",
          body: JSON.stringify({
            username: username.value,
            password: password.value,
          }),
          headers: {
            "Content-Type": "application/json",
          },
        })
          .then((res) => {
            // 获取到请求头 设置token
            localStorage.setItem("token", res.headers.get("Authorization"));
            return res.json();
          })
          .then((res) => {
            if (res.ok) {
              location.href = "/";
            } else {
              span.innerHTML = "密码用户名错误";
            }
          });
      });
    </script>
  </body>
</html>
